<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>6.3 继承</title>
    <script>
        function SuperType() {
            this.property = true;
            this.name = '花雨夜'; // 实例属性
        }

        SuperType.prototype.address = '重庆市';
        SuperType.prototype.getSuperValue = function () {
            return this.property;
        };

        function SubType() {
            this.subproperty = false;
        }

        SubType.prototype = new SuperType();
        SubType.prototype.getSubValue = function () {
            console.log('属性和方法都继承了：' + this.name);
            return this.subproperty;
        };

        let instance = new SubType();
        console.log(instance.getSuperValue());
        console.log(instance.getSubValue());
        console.log(instance.name);
        let instance2 = new SubType();
        console.log(instance2.name);
        instance2.name = '花花花花';
        console.log(instance2.name);
        console.log(instance.name);
        console.log('------------------------');
        console.log(instance2.hasOwnProperty('name')); // true 在实例中返回true
        console.log(instance2.address);
        console.log(instance2.hasOwnProperty('address')); // false
        SuperType.prototype.address = '北京市';
        console.log(instance2.address); // 原型属性和方法时共享的。修改了原型属性的值，继承了改对象的子类的原型属性都会修改。（）
        console.log((instance2 instanceof SuperType));


        console.log('==========================================');

        function SuperCat() {
            this.name = '动物';
            this.colors = ['red', 'blue', 'green'];
        }

        SuperCat.prototype.getName = function () {
            console.log('SuperCat.prototype.getName：' + this.name);
        };

        function SubCat() {
            this.home = '家园';
        }

        SubCat.prototype = new SuperCat();
        SubCat.prototype.getHome = function () {
            console.log('SubCat.prototype.getHome：' + this.home);
        };


        let tma = new SubCat();
        tma.getHome();
        tma.getName();
        console.log(tma.colors);
        console.log('==========================================');

        let tma1 = new SubCat();
        tma1.getHome();
        tma1.getName();
        console.log(tma1.colors);

        console.log('==========================================');
        tma.name = '好大的动物';
        tma.colors.push('black');
        tma.getName(); // 好大的动物
        tma1.getName(); // 动物
        console.log(tma.colors); // ["red", "blue", "green", "black"]
        console.log(tma1.colors); // ["red", "blue", "green", "black"]

    </script>
</head>
<body>

</body>
</html>

























